/**
 * Copyright (C) 2006 - present dabuTech Corporation  
 * All Rights Reserved.
 *
 * This file is part of Easier Java Persistence.
 *
 * EJP is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the accompanying license 
 * for more details.
 *
 * You should have received a copy of the license along with EJP; if not, 
 * go to http://www.EasierJava.com and download the latest version.
 */

package com.test;

import java.util.ArrayList;
import java.util.List;
import ejp.DatabaseManager;
import ejp.DatabaseException;
import ejp.PersistentClassManager;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

public class SimpleExample
  {
    public SimpleExample(DatabaseManager dbm) throws DatabaseException
      {
        // Clean out contacts
        dbm.executeUpdate("delete from contacts");

        // Inserting contact with associations
        Customer customer = new Customer("deisenhower", "mypasswd5", "Dwight", "Eisenhower", "United States", "deisenhower@unitedstates.gov");

        customer.getSupport().add(new Support("Request", "New", "no phone", "deisenhower@unitedstates.gov", "Can I have my bust on a dollar, please."));
        customer.getSupport().add(new Support("Response", "Pending", "no phone", "deisenhower@unitedstates.gov", "Yes, but you may have to share it."));
        customer.getSupport().add(new Support("Request", "New", "no phone", "deisenhower@unitedstates.gov", "Share it with who?"));

        customer.getOrders().add(new Order("Dwight D. Eisenhower Dollar", new Integer(100), new Double(1.00), "unverified"));
        customer.getOrders().add(new Order("Susan B. Anthony Dollar", new Integer(100), new Double(1.00), "unverified"));

        // Saving within an automatic transaction (covers all relationships)
        dbm.saveObject(customer);

        // Load based on information contained in classes
        customer = dbm.loadObject(Customer.class, "where :contactId = ?", "deisenhower");
        System.out.println("\ncontactId = " + customer.getUsername() + ", last name = " + customer.getLastName());
        
        // Load a collection of objects from the database
        Collection<Customer> c = dbm.loadObjects(new ArrayList<Customer>(), new Customer("deisenhower"));

        for (Customer customer2 : c)
          System.out.println("contactId = " + customer2.getUsername() + ", last name = " + customer2.getLastName());
      }

    public static void main(String[] args) throws DatabaseException
      {
        DatabaseManager dbm = null;
        Map<String, String> columnMapping = new HashMap<String, String>();

        columnMapping.put("username", "contact_id");

        PersistentClassManager.setTableMapping(Customer.class, "contacts");
        PersistentClassManager.setColumnMapping(Customer.class, columnMapping);

        try
          {
            DatabaseManager.setLogLevel(Level.WARNING);

            dbm = MemoryDatabase.getMemoryDatabase();
            //dbm = DatabaseManager.getDatabaseManager("ejp");
            //dbm = DatabaseManager.getDatabaseManager("ejp", 10,
            //      "com.mysql.jdbc.Driver", "jdbc:mysql://localhost/ejp_example");

            new SimpleExample(dbm);
          }
        finally
          {
            // also closes any open ejp.Database
            dbm.close();
          }
      }
    
    public static class Customer
      {
        private String username, password, firstName, lastName, companyName, email;
        private List<Support> support = new ArrayList<Support>();
        private List<Order> orders = new ArrayList<Order>();

        public Customer() {}

        public Customer(String username)
          {
            this.username = username;
          }

        public Customer(String username, String password, String firstName,
                       String lastName, String companyName, String email)
          {
            this.username = username;
            this.password = password;
            this.firstName = firstName;
            this.lastName = lastName;
            this.companyName = companyName;
            this.email = email;
          }

        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getPassword() { return password; }
        public void setPassword(String passwd) { password = passwd; }
        public String getFirstName() { return firstName; }
        public void setFirstName(String fName) { firstName = fName; }
        public String getLastName() { return lastName; }
        public void setLastName(String lName) { lastName = lName; }
        public String getCompanyName() { return companyName; }
        public void setCompanyName(String name) { companyName = name; }
        public String getEmail() { return email; }
        public void setEmail(String email) { this.email = email; }

        // Associations
        public List<Support> getSupport() { return support; }
        public void setSupport(List<Support> support) { this.support = support; }
        public List<Order> getOrders() { return orders; }
        public void setOrders(List<Order> orders) { this.orders = orders; }
      }
    
    public static class Order
      {
        private Long orderId;
        private Integer quantity;
        private Double price;
        private String contactId, product, status;

        public Order() {}

        public Order(String product, Integer quantity, Double price, String status)
          {
            this.product = product;
            this.quantity = quantity;
            this.price = price;
            this.status = status;
          }

        public Order(String contactId, String product, Integer quantity, Double price, String status)
          {
            this.contactId = contactId;
            this.product = product;
            this.quantity = quantity;
            this.price = price;
            this.status = status;
          }

        public Long getOrderId() { return orderId; }
        public void setOrderId(Long orderId) { this.orderId = orderId; }
        public String getContactId() { return contactId;}
        public void setContactId(String contactId) { this.contactId = contactId; }
        public String getProduct() { return product; }
        public void setProduct(String product) { this.product = product; }
        public Integer getQuantity() { return quantity; }
        public void setQuantity(Integer quantity) { this.quantity = quantity; }
        public Double getPrice() { return price; }
        public void setPrice(Double price) { this.price = price; }
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
      }
    
    public static class Support
      {
        private Long supportId;
        private String contactId, code, status, phone, email, request;

        public Support() {}

        public Support(String code, String status, String phone, String email, String request)
          {
            this.code = code;
            this.status = status;
            this.phone = phone;
            this.email = email;
            this.request = request;
          }

        public Support(String contactId, String code, String status, 
                       String phone, String email, String request)
          {
            this.contactId = contactId;
            this.code = code;
            this.status = status;
            this.phone = phone;
            this.email = email;
            this.request = request;
          }

        public Long getSupportId() { return supportId; }
        public void setSupportId(Long id) { supportId = id; }
        public String getContactId() { return contactId; }
        public void setContactId(String id) { contactId = id; }
        public String getCode() { return code; }
        public void setCode(String code) { this.code = code; }
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
        public String getPhone() { return phone; }
        public void setPhone(String phone) { this.phone = phone; }
        public String getEmail() { return email; }
        public void setEmail(String email) { this.email = email; }
        public String getRequest() { return request; }
        public void setRequest(String request) { this.request = request; }
      }
  }
